package ar.edu.itba.it.paw.web.product;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.behavior.SimpleAttributeModifier;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import ar.edu.itba.it.paw.domain.Auction;
import ar.edu.itba.it.paw.domain.Product;
import ar.edu.itba.it.paw.domain.User;
import ar.edu.itba.it.paw.repo.UserRepo;
import ar.edu.itba.it.paw.web.MercadoLibre;
import ar.edu.itba.it.paw.web.MercadoLibreSession;
import ar.edu.itba.it.paw.web.base.BasePage;
import ar.edu.itba.it.paw.web.component.TypeLabel;

public class EditProductListPage extends BasePage{

	@SpringBean
	UserRepo userRepo;
	
	public EditProductListPage(){
		IModel<List<Product>> productsModel = new LoadableDetachableModel<List<Product>>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			protected List<Product> load() {
				User user = MercadoLibreSession.get().getUser();
				return new ArrayList<Product>(user.getProducts_set());
			}
		};
		
		
		add(new PropertyListView<Product>("editProducts", productsModel) {
			
			@Override
			protected void populateItem(ListItem<Product> item) {
				item.add(new Label("name",new PropertyModel<Product>(item.getModel(), "name")).add(new SimpleAttributeModifier("style", "color: #0099FF")));
				item.add(new Label("description").add(new SimpleAttributeModifier("style", "")));
				item.add(new TypeLabel("type", item.getModel()));
				item.add(new Link<Product>("edit", item.getModel()) {
					@Override
					public void onClick() {
						setResponsePage(new EditProductPage(getModelObject()));
					}
				}.add(new Image("edit_image", MercadoLibre.EDIT_ICON).add(new SimpleAttributeModifier("height","15px"))));
			}
		});
		
		
	}
}
